package com.dd.admin.business.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.common.exception.ApiException;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.product.entity.Product;
import com.dd.admin.business.product.mapper.ProductMapper;
import com.dd.admin.business.product.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.dd.admin.business.product.domain.ProductVo;
import com.dd.admin.business.product.domain.ProductDto;
import java.util.List;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-05-23
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public IPage<ProductVo> selectProductPage(ProductDto productDto) {
        Page page = PageFactory.defaultPage();
        return baseMapper.selectProductPage(page,productDto);
    }

    @Override
    public List<ProductVo> selectProductList(ProductDto productDto) {
        return baseMapper.selectProductList(productDto);
    }

    @Override
    public Product addProductCount(String productId, Long count) {
        Product product = this.getById(productId);
        product.setProductCount(product.getProductCount()  +  count);
        updateById(product);
        return product;
    }

    @Override
    public Product subProductCount(String productId, Long count) {
        Product product = this.getById(productId);
        product.setProductCount(product.getProductCount()  -  count);
        if(product.getProductCount() < 0){
            throw new ApiException("商品库存不足");
        }
        updateById(product);
        return product;
    }


    /**
     * 根据机构shopId查询商品库存数量排行（可指定返回数量）
     *
     * @param shopId 机构id
     * @param limit  返回的排行数量限制（例如取前几名），如果为null则查询全部符合条件的商品排行
     * @return 按库存数量排序后的商品列表（库存数量从大到小排序）
     */
    @Override
    public List<Product> getProductStockRankByShopId(String shopId, Integer limit) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        // 根据shopId添加筛选条件，只查询指定机构下的商品
        queryWrapper.eq(Product::getShopId, shopId);
        // 按照库存数量字段（PRODUCT_COUNT）进行降序排序（从大到小）
        queryWrapper.orderByDesc(Product::getProductCount);

        if (limit!= null) {
            // 如果传入了数量限制，则添加限制条件，只取前limit条记录
            queryWrapper.last("LIMIT " + limit);
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer selectProductCount(String shopId) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getShopId, shopId);
        return baseMapper.selectCount(queryWrapper);
    }
}
